package selfview.sufutian.com.tvrecycle;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recycle_view;

    private ArrayList<DataModel> models=new ArrayList<>();

    private int  focusPosition;
    private MyRecycleAdapter recycleAdapter;
    private GridLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DataModel model;
        for(int i=0;i<5;i++){
            model=new DataModel();
            model.setImageId(R.drawable.image_2);
            model.setText("Item--"+(i+1));
            models.add(model);
        }

        recycle_view = (RecyclerView) findViewById(R.id.recycle_view);
        layoutManager = new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false);
        recycle_view.setLayoutManager(layoutManager);
        recycleAdapter = new MyRecycleAdapter();
        recycle_view.setAdapter(recycleAdapter);
    }


    private class MyRecycleAdapter extends RecyclerView.Adapter<MyHolder>{

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=View.inflate(MainActivity.this,R.layout.item_recycle,null);
            view.setFocusable(true);
            return new MyHolder(view);
        }

        @Override
        public void onBindViewHolder(final MyHolder holder, final int position) {
            DataModel model = models.get(position);
            holder.imageView.setImageResource(model.getImageId());
            holder.textView.setText(model.getText());
            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus){
                        focusPosition=position;
                        holder.focus_root.setBackgroundResource(R.drawable.focus_bg);
                    }else{
                        holder.focus_root.setBackgroundColor(Color.TRANSPARENT);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return models.size();
        }

    }


    private class MyHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView textView;
        LinearLayout focus_root;
        public MyHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.image_view);
            textView= (TextView) itemView.findViewById(R.id.text_view);
            focus_root= (LinearLayout) itemView.findViewById(R.id.focus_root);
        }
    }



    private class DataModel{
        int imageId;
        String text;

        public int getImageId() {
            return imageId;
        }

        public void setImageId(int imageId) {
            this.imageId = imageId;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
    int count=1;
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode()==KeyEvent.KEYCODE_DPAD_RIGHT){
            int size = models.size();
            if(focusPosition==models.size()-1){
                DataModel model;
                for(int i=0;i<5;i++){
                    model=new DataModel();
                    model.setImageId(R.drawable.image_2);
                    model.setText("过年回家啦--"+count+"-loadmore-"+(i+1));
                    models.add(model);
                }
                count++;
                recycleAdapter.notifyItemRangeChanged(size,5);
            }
        }
        return super.dispatchKeyEvent(event);
    }
}
